package com.zky.gulimall.product;

import java.util.concurrent.TimeUnit;

/**
 * @Description TODO
 * @Author 张开源
 * @Date 2022/11/11 22:09
 * @Version 1.0
 **/
public class DeadLockDemo {

    public static void main(String[] args) {
        final Object objectA=new Object();
        final Object objectB=new Object();
        new Thread(()->{
            synchronized (objectA){
                System.out.println(Thread.currentThread().getName()+"\t自己持有A锁，希望获取B锁");
                try {
                    TimeUnit.SECONDS.sleep(1);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
                synchronized (objectB){
                    System.out.println(Thread.currentThread().getName()+"\t成功获取B锁");
                }
            }
        },"A").start();

        new Thread(()->{
            synchronized (objectB){
                System.out.println(Thread.currentThread().getName()+"\t自己持有B锁，希望获取A锁");
                try {
                    TimeUnit.SECONDS.sleep(1);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
                synchronized (objectA){
                    System.out.println(Thread.currentThread().getName()+"\t成功获取A锁");
                }
            }
        },"B").start();
    }
}
